package com.ruoyi.system.service.impl;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateTime;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SjImportRecordMapper;
import com.ruoyi.system.mapper.SjRadomStandardMapper;
import com.ruoyi.system.mapper.SjRandomRecordMapper;
import com.ruoyi.system.utils.FileToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SjStandardMapper;
import com.ruoyi.system.service.ISjStandardService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 双随机标准Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-02
 */
@Service
public class SjStandardServiceImpl implements ISjStandardService 
{
    private static final Logger logger = LoggerFactory.getLogger(SjStandardServiceImpl.class);

    @Autowired
    private SjStandardMapper sjStandardMapper;

    @Autowired
    private SjImportRecordMapper sjImportRecordMapper;

    @Autowired
    private SjRandomRecordMapper sjRandomRecordMapper;

    @Autowired
    private SjRadomStandardMapper sjRadomStandardMapper;

    /**
     * 查询双随机标准
     * 
     * @param id 双随机标准主键
     * @return 双随机标准
     */
    @Override
    public SjStandard selectSjStandardById(Long id)
    {
        return sjStandardMapper.selectSjStandardById(id);
    }

    /**
     * 查询双随机标准
     *
     * @param identify 双随机标准唯一标识
     * @return 双随机标准
     */
    @Override
    public SjStandard selectSjStandardListByIdentify(String identify)
    {
        return sjStandardMapper.selectSjStandardListByIdentify(identify);
    }

    /**
     * 查询双随机标准列表
     * 
     * @param sjStandard 双随机标准
     * @return 双随机标准
     */
    @Override
    public List<SjStandard> selectSjStandardList(SjStandard sjStandard)
    {
        return sjStandardMapper.selectSjStandardList(sjStandard);
    }

    /**
     * 新增双随机标准
     * 
     * @param sjStandard 双随机标准
     * @return 结果
     */
    @Override
    public int insertSjStandard(SjStandard sjStandard)
    {
        return sjStandardMapper.insertSjStandard(sjStandard);
    }

    /**
     * 修改双随机标准
     * 
     * @param sjStandard 双随机标准
     * @return 结果
     */
    @Override
    public int updateSjStandard(SjStandard sjStandard)
    {
        return sjStandardMapper.updateSjStandard(sjStandard);
    }

    /**
     * 批量删除双随机标准
     * 
     * @param ids 需要删除的双随机标准主键
     * @return 结果
     */
    @Override
    public int deleteSjStandardByIds(Long[] ids)
    {
        return sjStandardMapper.deleteSjStandardByIds(ids);
    }

    /**
     * 删除双随机标准信息
     * 
     * @param id 双随机标准主键
     * @return 结果
     */
    @Override
    public int deleteSjStandardById(Long id)
    {
        return sjStandardMapper.deleteSjStandardById(id);
    }

    @Override
    public AjaxResult importData(String filePath, String category) {
        //记录开始读取Excel时间,也是导入程序开始时间
        int addNum = 0;
        int upNum = 0;
        long startReadTime = System.currentTimeMillis();
        List<SjStandard> list = new LinkedList<>();
        try {
            EasyExcel.read(new File(filePath))
            .head(SjStandard.class)
            .sheet()
            .registerReadListener(new AnalysisEventListener<SjStandard>() {
                @Override
                public void invoke(SjStandard integerStringMap, AnalysisContext analysisContext) {
                    list.add(integerStringMap);
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                    logger.info("数据读取完毕");
                }
            }).doRead();
            for (SjStandard sjStandard : list) {
                //判断重复
                SjStandard standard = sjStandardMapper.selectSjStandardListByIdentify(sjStandard.getIdentifying());
                if(standard!=null){
                    //更新
                    sjStandard.setId(standard.getId());
                    sjStandard.setCreateTime(standard.getCreateTime());
                    sjStandard.setUpdateDate(DateTime.now());
                    sjStandardMapper.updateSjStandard(sjStandard);
                    upNum++;
                }else{
                    //插入
                    sjStandard.setCreateTime(DateTime.now());
                    sjStandard.setUpdateDate(DateTime.now());
                    sjStandard.setCategoryName(category);
                    sjStandard.setIsRandom("0");
                    sjStandardMapper.insertSjStandard(sjStandard);
                    addNum++;
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        long endReadTime = System.currentTimeMillis();
        //插入日志
        SjImportRecord sjImportRecord = new SjImportRecord();
        sjImportRecord.setImportnum((long)list.size());
        sjImportRecord.setCreatedate(DateTime.now());
        sjImportRecord.setInsertnum((long)addNum);
        sjImportRecord.setUpdatenum((long)upNum);
        sjImportRecordMapper.insertSjImportRecord(sjImportRecord);
        Map<String,String> map = new HashMap<>();
        map.put("totalTime",(endReadTime-startReadTime)+"");
        map.put("upNum",upNum+"");
        map.put("adNum",addNum+"");
        map.put("totalNum",list.size()+"");
        return AjaxResult.success("导入成功",map);
    }


    @Override
    public List<SjStandard> randomData(RandomVo randomVo){
        //抽取集合
        List<SjStandard> list = sjStandardMapper.randomData(randomVo);
        //插入记录
        SjRandomRecord sjRandomRecord = new SjRandomRecord();
        sjRandomRecord.setId(UUID.randomUUID().toString());
        sjRandomRecord.setIsRandom(randomVo.isaRandom);
        sjRandomRecord.setRandomNum((long)randomVo.randomNum);
        sjRandomRecord.setCategoryName(randomVo.categoryName);
        sjRandomRecord.setCreateDate(DateTime.now());
        sjRandomRecord.setA101(randomVo.randomDate);
        sjRandomRecordMapper.insertSjRandomRecord(sjRandomRecord);
        //插入附属标准
        for(SjStandard standard:list){
            SjRadomStandard sjRadomStandard = new SjRadomStandard();
            sjRadomStandard.setStandIden(standard.getIdentifying());
            sjRadomStandard.setRecordId(sjRandomRecord.getId());
            sjRadomStandard.setA100(standard.getA100());
            sjRadomStandard.setA298(standard.getA298());
            sjRadomStandardMapper.insertSjRadomStandard(sjRadomStandard);
            //抽取状态修改
            standard.setIsRandom("1");
            sjStandardMapper.updateSjStandard(standard);
        }
        return list;
    }

    /**
     * 数量统计
     *
     * @param
     * @return 结果
     */
    public String countStandard(){
        return sjStandardMapper.countStandard();
    }
}
